package com.framework.controller.newcontroller;

import com.framework.entity.sys.SysAddress;
import com.framework.exception.util.RespResult;
import com.framework.service.AddressService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * 功能描述: 收货地址控制器
 *
 * @author ck
 * @date 2018/9/18 11:48
 */
@RestController
@Api(value="收货地址模块",tags={"收货地址模块"})
@RequestMapping(value = "new/address")
public class AddressNewController {

	@Autowired
	private AddressService addressService;

	/**
	 * 获取当前人收货地址列表
	 * @param token 登录产生的token
	 * @return
	 */
	@GetMapping("user/queryAddressList")
	@ApiOperation("获取当前人收货地址列表")
	public RespResult queryAddressList(String token) {
		List<SysAddress> list = addressService.queryAddressList(token);
		return RespResult.ok().put("addressList",list);
	}

	/**
	 * 添加收货地址
	 * @param address 地址对象
	 * @param token 登录人token
	 * @return
	 */
	@PostMapping("user/insertAddress")
	@ApiOperation("添加收货地址")
	public RespResult insertAddress(SysAddress address, String token) {
		addressService.insertAddress(address,token);
		return RespResult.ok();
	}

	/**
	 * 修改收货地址
	 * @param address 对象
	 * @param token 登录人token
	 * @return
	 */
	@PostMapping("user/updateAddress")
	@ApiOperation("修改收货地址")
	@ApiImplicitParams({
			@ApiImplicitParam(name="addressId",value="地址id",dataType="string", paramType = "query" ,required = true),
	})
	public RespResult updateAddress(SysAddress address,String token) {
		addressService.updateAddress(address,token);
		return RespResult.ok();
	}



	/**
	 * 设置默认地址
	 * @param addressId 对象
	 * @param token 登录人token
	 * @return
	 */
	@PostMapping("user/setDefaultAddress")
	@ApiOperation("设置默认地址")
	@ApiImplicitParams({
			@ApiImplicitParam(name="addressId",value="地址id",dataType="string", paramType = "query" ,required = true),
			@ApiImplicitParam(name="token",value="登录产生的token",dataType="string", paramType = "query" ,required = true)
	})
	public RespResult setDefaultAddress(String addressId,String token) {
		addressService.infoAddress(token,addressId);
		addressService.setDefault(addressId);
		return RespResult.ok();
	}

	/**
	 * 获取地址详情信息
	 * @param token 登录人token
	 * @param addressId id
	 * @return
	 */
	@GetMapping("user/infoAddressById")
	@ApiOperation("获取地址详情信息")
	@ApiImplicitParams({
			@ApiImplicitParam(name="addressId",value="地址id",dataType="string", paramType = "query" ,required = true),
	})
	public RespResult infoAddress(String token,String addressId) {
		SysAddress info = addressService.infoAddress(token,addressId);
		return RespResult.ok().put("address",info);
	}

	/**
	 * 查询代理商默认的收货地址
	 * @param token 登录的token
	 * @return
	 */
	@GetMapping("user/queryDefaultAddress")
	@ApiOperation("查询代理商默认的收货地址")
	public RespResult queryDefaultAddress(String token) {
		SysAddress info = addressService.queryDefaultAddress(token);
		return RespResult.ok().put("info",info==null?new SysAddress():info);
	}

	/**
	 * 删除地址
	 * @param addressId 地址id
	 * @return
	 */
	@PostMapping("user/delAddress")
	@ApiOperation("删除收货地址")
	@ApiImplicitParams({
			@ApiImplicitParam(name="addressId",value="地址id",dataType="string", paramType = "query" ,required = true),
	})
	public RespResult delAddress(String addressId) {
		addressService.delAddress(addressId);
		return RespResult.ok();
	}
}
